#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include<QTimer>
#include<QLabel>
#include<QDebug>
#include<QSound>
#include "mypushbutton.h"
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //配置选择关卡场景
    this->setFixedSize(400,710);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("选择关卡场景");
    //创建菜单栏
    QMenuBar *bar=menuBar();
    this->setMenuBar(bar);
    QMenu *startMenu=bar->addMenu("开始");
    //退出菜单项
    QAction *quitAction=startMenu->addAction("退出");
    //退出
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //选择关卡按钮音效
    QSound *chooseSound=new QSound(":/res/TapButtonSound.wav",this);
    //返回按钮音效
    QSound *backSound=new QSound(":/res/BackButtonSound.wav",this);
    //返回按钮
    MyPushButton *backBtn =new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //点击返回
    connect(backBtn,&MyPushButton::clicked,[=](){
        backSound->play();
        QTimer::singleShot(50,this,[=](){
            emit this->ChooseSceneBack();
        });
    });

    //创建选择关卡按钮
    for(int i=0;i<20;i++){
        MyPushButton *menuBtn=new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        //i%4 x坐标每一列相同 y坐标每一行相同 50每个按钮间距
        menuBtn->move(70+i%4*70,150+i/4*70);
        //监听每个按钮点击事件
        connect(menuBtn,&MyPushButton::clicked,[=](){
            //播放选择关卡音效
            chooseSound->play();

            QString string=QString("你选择第 %1 关卡").arg(i+1);
            qDebug()<<string;

            // 进入游戏场景
            this->hide();//选择关卡隐藏
            play=new PlaySceen(i+1);
            play->setGeometry(this->geometry());
            play->show();    
            connect(play,&PlaySceen::ChooseSceneBack,[=](){
                this->setGeometry(play->geometry());
                this->show();
                delete play;
                play=NULL;
            });
        });

        QLabel *label=new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(70+i%4*70,150+i/4*70);
        //设置文字的对齐方式 水平居中和垂直居中
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        //设置让鼠标穿透 label覆盖了button
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题
    pix.load(":/res/Title.png");
//    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);

}
